#include "collapseview.h"
#include "ui_collapseview.h"


#include "collapseviewitem.h"
#include <QDebug>
#include <QMessageBox>


CollapseView::CollapseView(int wsfd,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CollapseView)
{
    ui->setupUi(this);

    m_wsfd=wsfd;
/*
    collapsGroup.clear();
    CollapseViewItem *lineUser=new CollapseViewItem("在线",this) ;
    ui->widget->layout()->addWidget(lineUser);
    collapsGroup.append(lineUser);
    connect(lineUser,SIGNAL(sigTalkWindow(QString)),this,SLOT(onTalkWindowItemClicked(QString)));
*/
   //ui->widget->layout()->addWidget(new CollapseViewItem("离线",this));

    //ui->widget->layout()->addWidget(new CollapseViewItem("那些年",this));

    //ui->widget->layout()->addWidget(new CollapseViewItem("陌生人",this));



    ui->scrollAreaWidgetContents->resize(this->width(),this->height());

    ui->scrollArea->resize(this->width(),this->height());
    m_clientMap.clear();

}

CollapseView::~CollapseView()
{
    foreach(CollapseViewItem *var,collapsGroup)
    {
        delete var;
    }
    collapsGroup.clear();
    m_clientMap.clear();
    delete ui;
}

void CollapseView::butClick()
{
    //ui->listWidget->setHidden(!ui->listWidget->isHidden());
    qDebug()<<"xxx";
}
/**
 * @brief CollapseView::addItem 添加项
 * @param uuid
 * @param text
 * @return
 */
LitterIem* CollapseView::addItem(int fd,QString uuid,QString text,QString mood)
{
    //qDebug()<<text;
     //   qDebug()<<"线程ID"<<pthread_self();
    //随机选择好友头像
   int MAX_PERSON_PIC_NUM2=9;
    QString picPath=QString(":/media/person/media/person/%1.jpg")
            .arg(QString::number(qrand()%(MAX_PERSON_PIC_NUM2-1)+1));

    LitterIem *it=new LitterIem(picPath,this);
    it->setObjectName(uuid);
    it->displayNickname(text);
    it->setUUID(uuid);
    it->setFd(fd);
    it->displayMood(mood);
    ui->widget->layout()->addWidget(it);
    connect(it,SIGNAL(sigTalkWindow(int,QString)),this,SLOT(onTalkWindowItemClicked(int,QString)));
    //qDebug()<<it->objectName()<<"\tuuid:"<<uuid;
    //connect(it,SIGNAL(clicked()),this,SLOT(onChat(QWidget *)));
    //connect(ui->listWidget,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(onChat(QListWidgetItem*)));//&LitterIem::onTalkWindowItemClicked);


    return it;
}
/**
 * @brief CollapseView::addCollapse 添加一个用户
 * @param fd
 * @param uuid
 * @param nickname
 */
void CollapseView::addCollapse(int fd,QString uuid,QString nickname,QString mood)
{

    //qDebug()<<uuid;
    if(!m_clientMap.contains(uuid)){
    clientItem *ci=new clientItem();
    ci->fd=fd;
    ci->litter=NULL;
    ci->nickname=nickname;
    ci->talkwnd=NULL;
    ci->uuid=uuid;    
    ci->litter=addItem(fd,uuid,nickname,mood);
        m_clientMap.insert(uuid,ci);
    }
    else
    {
        m_clientMap[uuid]->nickname=nickname;
        m_clientMap[uuid]->uuid=uuid;
        m_clientMap[uuid]->mood=mood;
        if(m_clientMap[uuid]->litter!=NULL)
        {
            m_clientMap[uuid]->litter->displayMood(mood);
            m_clientMap[uuid]->litter->displayNickname(nickname);
        }
    }
}
void CollapseView::closeCollapses()
{
    foreach (clientItem *var, m_clientMap) {
        chatForm *cf=var->talkwnd;
        if(cf!=NULL)
            cf->close();
    }
}

void CollapseView::delCollapse(int fd,QString uuid,QString nickname)
{
    if(m_clientMap.contains(uuid)){
        m_clientMap[uuid]->litter->close();
        ui->widget->layout()->removeWidget(m_clientMap[uuid]->litter);
    }
    m_clientMap.remove(uuid);
}


void CollapseView::readMessage(WS_MESSAGE msg)
{
    if(m_clientMap.contains(msg.f_uuid)){

        chatForm *wnd=m_clientMap[msg.f_uuid]->talkwnd;
        if(wnd!=NULL)
        {
            wnd->ReceivedMessage(msg.message);
        }
        else{

            wnd=new chatForm(m_clientMap[msg.f_uuid]->nickname,msg.f_uuid,msg.f_fd,NULL);
            m_clientMap[msg.f_uuid]->talkwnd=wnd;
            wnd->hide();
            wnd->ReceivedMessage(msg.message);
        }
        }
}
void CollapseView::onThemeColorChange(QString colorStr)
{
    foreach (clientItem *var, m_clientMap) {
        chatForm *cf=var->talkwnd;
        if(cf!=NULL)
            cf->onThemeColorChange(colorStr);
    }

}
/**
 * @brief CollapseView::onTalkWindowItemClicked 响应聊天
 * @param uuid
 */
void CollapseView::onTalkWindowItemClicked(int  fd,QString uuid)
{
    //qDebug()<<"onTalkWindowItemClicked()"<<uuid;
    if(m_clientMap.contains(uuid)){

        chatForm *wnd=m_clientMap[uuid]->talkwnd;
        if(wnd==NULL)
        {


            wnd=new chatForm(m_clientMap[uuid]->nickname,m_clientMap[uuid]->uuid,m_clientMap[uuid]->fd,NULL);
            m_clientMap[uuid]->talkwnd=wnd;
        }
        //qDebug()<<m_clientMap[uuid]->talkwnd;
        m_clientMap[uuid]->talkwnd->show();
    }
}

